/**
 * 
 */
package upload;

/**
 * @author yangshengfei
 * @createTime 2015年12月17日
 * @updateTime 2015年12月17日
 * @描述:
 */

import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;

public class uploadImage {
	
	private static String accessKey="mubtPbRCbGuFSKm5Sp3edlyQb3lY5bTaMjUdfWsu";
	private static String secretKey="rcFPp8VCppIJtn2gP8fGMS1xidmeEf2dqYB4SQUN";
	private static String bucketName="myprintphoto";

	/**
	 * 
	 */
	public uploadImage() {
		// TODO Auto-generated constructor stub
	}
	
	public class MyRet {
	    public long fsize;
	    public String key;
	    public String hash;
	    public int width;
	    public int height;
	}
	
	public String getToken() {
		Auth auth = Auth.create(accessKey, secretKey);
		return auth.uploadToken(bucketName);
	}
	
	public void PrintAppUP() {
		 UploadManager uploadManager = new UploadManager();
		 Auth auth = Auth.create(accessKey, secretKey);
		 String token = auth.uploadToken(bucketName);
		 try {
		        Response res = uploadManager.put("helllo".getBytes(), accessKey, token);
		        MyRet ret = res.jsonToObject(MyRet.class);
		        
		        log(res.toString());
		        log(res.bodyString());
		 } catch (QiniuException e) {
		        Response r = e.response;
		        // 请求失败时简单状态信息
		        log(r.toString());
		        try {
		            // 响应的文本信息
		            log(r.bodyString());
		        } catch (QiniuException e1) {
		            //ignore
		        }
		 }

	}
	
	public static void log(String s) {
		System.out.print(s);
	}
	
	public static void main(String[] args) {
		UploadManager uploadManager = new UploadManager();
		 Auth auth = Auth.create(accessKey, secretKey);
		 String token = auth.uploadToken(bucketName);
		 try {
		        Response res = uploadManager.put("hello".getBytes(), accessKey, token);
		        MyRet ret = res.jsonToObject(MyRet.class);
		        
		        //log(res.toString());
		        log(res.bodyString());
		 } catch (QiniuException e) {
		        Response r = e.response;
		        // 请求失败时简单状态信息
		        log(r.toString());
		        try {
		            // 响应的文本信息
		            log(r.bodyString());
		        } catch (QiniuException e1) {
		            //ignore
		        }
		 }
	}

}
